Debug

Comments
  • .

Proper printing
  • [#derive(Debug)]  + "println!({:?}, x)"

    #[derive(Debug)]
    struct Rectangle {
        width: u32,
        height: u32,
    }
    
    impl Rectangle {
        fn area(&self) -> u32 {
            self.width * self.height
        }
    }
    
    fn main() {
        let rect1 = Rectangle {
            width: 30,
            height: 50,
        };
    
        println!(
            "The area of the rectangle is {} square pixels.",
            rect1.area()
        );
    }
    
VSCode Debug
  • LLDB extension installed.

  • launch.json file:

    • .

    • .

      • Used from the Native Debug Extension.

Error Handling
  • panic!

    • Crashes the program.

    • Avoid using it.

  • assert!()

    • Panics if the given condition is false.

  • .unwrap()

    • .

  • .expect()

    • .

LSP
Tests
  • #[cfg(test)]

    • Used to mark code blocks  that should be compiled only during testing .

    • Modules marked with this attribute are not compiled into the binary.

    • Where used :

      • Usually in modules ( mod ) or other sections of code that should only be included in the binary during testing.

    • Behavior :

      • Any code within a block marked with #[cfg(test)]  will be ignored during normal compilation (not included in the final binary) and only compiled/executed during tests.

  • #[test]

    • Used to mark a function  as a test.

    • Functions marked with this attribute are not compiled into the binary.

    • Where used :

      • Directly before a function to be executed by Rust’s test framework.

    • Behavior :

      • The test framework recognizes functions with #[test]  as individual tests and executes them when running cargo test .

  • cargo test

    • Runs all tests.